package com.inch.web.resolver;

import com.inch.web.exceptions.MyException1;
import com.inch.web.exceptions.MyException2;
import com.inch.web.exceptions.MyException3;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if(e instanceof MyException1) {
            modelAndView.setViewName("redirect:/page/error/error1.html");
        } else if(e instanceof MyException2) {
            modelAndView.setViewName("redirect:/page/error/error2.html");
        } else if(e instanceof MyException3) {
            modelAndView.setViewName("redirect:/page/error/error3.html");
        } else if(e instanceof MaxUploadSizeExceededException){
            modelAndView.setViewName("redirect:/upload/uploadDownload");
            modelAndView.addObject("message","Upload Failed, one or more files exceed size limit.");
        } else {
                    e.printStackTrace();
        }
        return modelAndView;
    }
}
